package com.wnhz.wnmap.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class HttpResp<T> implements Serializable {

    private int code;
    private String msg;
    private T data;
    private Date time;

    /**
     * 成功无返回结果
     *
     * @return HttpResp
     */
    public static <T> HttpResp<T> success() {
      return success(null);
    }

    /**
     * 成功， 有返回结果
     *
     * @param data 返回数据
     * @return 返回HttpResp对象
     */
    public static <T> HttpResp<T> success(T data) {
        RespCode success = RespCode.SUCCESS;
        return new HttpResp(
                success.getCode(),
                success.getMsg(),
                data,
                new Date());
    }

    /**
     * 失败自定义返回错误信息
     *
     * @return 返回HttpResp对象
     */
    public static <T> HttpResp<T> failed(String msg) {
        return new HttpResp(
                RespCode.ERROR.getCode(),
                msg,
                null,
                new Date());
    }

    /**
     * 失败
     *
     * @return 返回HttpResp对象
     */
    public static <T> HttpResp<T> failed(RespCode respCode) {
        return new HttpResp(
                respCode.getCode(),
                respCode.getMsg(),
                null,
                new Date());
    }

}
